package protocol.JT808_2011;

import java.util.HashMap;

public class Handle808_2011JT83 implements IHandle808_2011JT {
    @Override
    public String handle(JT808_2011Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x00:
                return handle8300(cmd);
            case 0x01:
                return handle8301(cmd);
            case 0x02:
                return handle8302(cmd);
            case 0x03:
                return handle8303(cmd);
            case 0x04:
                return handle8304(cmd);
            default:
                return "808-2011:未匹配的命令字"+(cmd.readOrder());
        }
    }

    private String handle8304(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:信息服务");
        builder.append("\r\n");

        builder.append("信息类型:"+cmd.readInt(1));
        builder.append("\r\n");

        builder.append("信息名称:"+cmd.readString(cmd.readWORD()));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle8303(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:信息点播菜单设置");
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"删除终端全部信息项");
        list.put(1,"更新菜单");
        list.put(2,"追加菜单");
        list.put(3,"修改菜单");
        int type = cmd.readInt(1);
        builder.append("设置类型("+type+"):"+list.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        int size = cmd.readInt(1);
        for (int i = 0;i<size;i++){
            builder.append("信息类型:"+cmd.readInt(1));
            builder.append("\r\n");

            builder.append("信息名称:"+cmd.readString(cmd.readWORD()));
            builder.append("\r\n");
        }

        return builder.toString();
    }

    private String handle8300(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:文本信息下发");
        builder.append("\r\n");

        int type = cmd.readInt(4);
        builder.append("标志："+type);
        builder.append("\r\n");
        builder.append("    紧急："+(type&0x01));
        builder.append("\r\n");
        builder.append("    预留"+(type>>1&0x01));
        builder.append("\r\n");
        builder.append("    终端显示器显示："+(type>>2&0x01));
        builder.append("\r\n");
        builder.append("    终端语音合成播读："+(type>>3&0x01));
        builder.append("\r\n");
        builder.append("    广告屏显示："+(type>>4&0x01));
        builder.append("\r\n");

        builder.append("文本信息："+cmd.readString(cmd.leftSize()));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle8301(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:事件设置");
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"删除终端现有所有事件");
        list.put(1,"更新事件");
        list.put(2,"追加事件");
        list.put(3,"修改事件");
        list.put(4,"删除特定几项事件");
        int type = cmd.readInt(1);
        builder.append("设置类型("+type+"):"+list.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        if (type == 0){
            return builder.toString();
        }

        int size = cmd.readInt(1);
        for (int i = 0;i<size;i++){
            builder.append("事件ID:"+cmd.readInt(1));
            builder.append("\r\n");
            if (type != 4){
                builder.append("事件内容:"+cmd.readString(cmd.readInt(1)));
                builder.append("\r\n");
            }
        }

        return builder.toString();
    }


    private String handle8302(JT808_2011Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:提问下发");
        builder.append("\r\n");
        int type = cmd.readInt(1);
        builder.append("\r\n");
        builder.append("标志："+type);
        builder.append("\r\n");
        builder.append("    紧急："+(type&0x01));
        builder.append("\r\n");
        builder.append("    预留"+(type>>1&0x01));
        builder.append("\r\n");
        builder.append("    终端显示器显示："+(type>>2&0x01));
        builder.append("\r\n");
        builder.append("    终端语音合成播读："+(type>>3&0x01));
        builder.append("\r\n");
        builder.append("    广告屏显示："+(type>>4&0x01));
        builder.append("\r\n");

        int len = cmd.readInt(1);
        builder.append("问题长度:"+len);
        builder.append("\r\n");
        builder.append("问题:"+cmd.readString(len));
        builder.append("\r\n");
        builder.append("候选答案列表:");
        builder.append("\r\n");
        while (cmd.leftSize()>1){
            builder.append("    答案ID:"+cmd.readInt(1));
            builder.append("\r\n");
            builder.append("    答案内容:"+cmd.readString(cmd.readInt(1)));
            builder.append("\r\n");
        }

        return builder.toString();
    }

}
